package com.ruoyi.web.controller.custom;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.custom.domain.vo.CustomVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.custom.domain.Custom;
import com.ruoyi.custom.service.ICustomService;

import static org.apache.commons.lang3.SystemUtils.getUserName;

/**
 * 客户Controller
 *
 * @author ruoyi
 * @date 2025-07-24
 */
@Api(tags = "客户")
@RestController
@RequestMapping("/custom/custom")
public class CustomController extends BaseController
{
    @Autowired
    private ICustomService customService;

    /**
     * 查询客户列表
     */
    @ApiOperation("查询客户列表分页")
    @PreAuthorize("@ss.hasPermi('custom:custom:list')")
    @GetMapping("/list")
    public TableDataInfo list(Custom custom)
    {
        startPage();
        List<CustomVo> list = customService.selectCustomList(custom);
        return getDataTable(list);
    }

    @ApiOperation("查询客户列表分页权限")
    @PreAuthorize("@ss.hasPermi('custom:custom:list')")
    @GetMapping("/listData")
    public TableDataInfo listData(Custom custom)
    {
        startPage();
        List<CustomVo> list = customService.selectCustomListData(custom);
        return getDataTable(list);
    }
    @ApiOperation("查询所有数据")
    @PreAuthorize("@ss.hasPermi('custom:custom:list')")
    @GetMapping("/allList")
    public AjaxResult allList(Custom custom)
    {
        List<CustomVo> list = customService.selectCustomList(custom);
        return AjaxResult.success(list);
    }

    /**
     * 导出客户列表
     */
    @PreAuthorize("@ss.hasPermi('custom:custom:export')")
    @Log(title = "客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,Custom custom)
    {
        List<CustomVo> list = customService.selectCustomList(custom);
        ExcelUtil<CustomVo> util = new ExcelUtil<CustomVo>(CustomVo.class);
         util.exportExcel(response,list, "客户数据");
    }

    /**
     * 获取客户详细信息
     */
    @ApiOperation("获取客户详细信息")
    @PreAuthorize("@ss.hasPermi('custom:custom:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(customService.selectCustomById(id));
    }

    /**
     * 新增客户
     */
    @ApiOperation("新增客户")
    @PreAuthorize("@ss.hasPermi('custom:custom:add')")
    @Log(title = "客户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Custom custom)
    {
        custom.setNo(DateUtils.generate());
        custom.setCreateTime(DateUtils.getNowDate());
        custom.setCreateBy(getUserName());
        return toAjax(customService.save(custom));
    }

    /**
     * 修改客户
     */
    @ApiOperation("修改客户")
    @PreAuthorize("@ss.hasPermi('custom:custom:edit')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Custom custom)
    {
        return toAjax(customService.updateCustom(custom));
    }

    /**
     * 删除客户
     */
    @ApiOperation("删除客户")
    @PreAuthorize("@ss.hasPermi('custom:custom:remove')")
    @Log(title = "客户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(customService.deleteCustomByIds(ids));
    }

    /**
     * 分派客戶
     */
    @ApiOperation("批量分派客戶")
    @PreAuthorize("@ss.hasPermi('custom:custom:send')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PostMapping("/send")
    public AjaxResult send(@RequestBody CustomVo custom)
    {
        return toAjax(customService.sendCustom(custom));
    }

    @ApiOperation("批量领取")
    @PreAuthorize("@ss.hasPermi('custom:custom:get')")
    @Log(title = "客户", businessType = BusinessType.UPDATE)
    @PostMapping("/getCustoms")
    public AjaxResult getCustoms(@RequestBody CustomVo custom)
    {
        return toAjax(customService.getCustom(custom));
    }
}